package com.tuzhi.sys.scurity.impl;

import com.tuzhi.base.web.util.ServletUtil;
import com.tuzhi.common.domain.LoginUserBO;
import com.tuzhi.common.exception.BusinessException;
import com.tuzhi.sys.jpa.entity.SysUserEntity;
import com.tuzhi.sys.scurity.AuthTokenService;
import com.tuzhi.sys.scurity.TokenStoreService;
import com.tuzhi.sys.service.SysUserService;
import com.tuzhi.util.StringTzUtil;
import lombok.extern.slf4j.Slf4j;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
@Slf4j
public class AuthTokenServiceImpl implements AuthTokenService {
    @Autowired
    TokenServiceImpl tokenService;
    @Autowired
    TokenStoreService tokenStoreService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public LoginUserBO login(String userName, String pwd, int loginType, String mobile) {
        if (StringTzUtil.isBlankNull(userName)) {
            throw new BusinessException("用户不能为空");
        }
        if (StringTzUtil.isBlankNull(pwd)) {
            throw new BusinessException("密码不能为空");
        }
        SysUserEntity entity;
        if (loginType == 0) {
            entity = sysUserService.getUserByUserName(userName);
        } else {
            entity = sysUserService.getUserByMobile(mobile);
        }
        if (entity == null) {
            log.info("{}:登录时，帐号不存在", userName);
            throw new BusinessException("帐号不存在");
        }
        boolean flag = BCrypt.checkpw(pwd, entity.getPassword());
        if (!flag) {
            log.info("{}:登录时，密码错误", userName);
            throw new BusinessException("登录时，密码错误");
        }
        LoginUserBO loginUserBO = this.createLoginUser(entity);
        String token = tokenService.createToken(loginUserBO);//内在缓存处理
        loginUserBO.setToken(token);
        return loginUserBO;
    }

    @Override
    public void loginOut() {
        HttpServletRequest request = ServletUtil.getRequest();
        LoginUserBO loginUserBO = this.tokenService.getLoginUser(request);
        String uuid;
        if (loginUserBO == null) {
            uuid = this.tokenService.getToken(request);
            log.warn("登出请求时，在服务端找不到对应的用户信息，服务端己失效：{}", uuid);
        } else {
            uuid = loginUserBO.getJti();
            this.tokenService.delLoginUser(uuid);
        }
    }

    /**
     * 获取用户身份信息
     *
     * @return 用户信息
     */
    public LoginUserBO getLoginUser(HttpServletRequest request) {
        // 获取请求携带的令牌
        String token = tokenService.getToken(request);
        return tokenService.getLoginUser(token);
    }

    /**
     * 获取用户身份信息
     *
     * @return 用户信息
     */
    public LoginUserBO getLoginUser(String token) {
        return tokenService.getLoginUser(token);
    }
    protected LoginUserBO createLoginUser(SysUserEntity userEntity) {
        LoginUserBO loginUserBO = new LoginUserBO();
        loginUserBO.setUserId(userEntity.getId());
        loginUserBO.setDeptId(userEntity.getOrgId());
        loginUserBO.setOrgId(userEntity.getOrgId());
        loginUserBO.setNickName(userEntity.getNickname());
        loginUserBO.setTel(userEntity.getPhone());
        return loginUserBO;
    }

}
